package com.yuan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yuan.pojo.dto.EmployyDto;
import com.yuan.pojo.dto.JybData;
import com.yuan.pojo.dto.JybDataDto;
import com.yuan.pojo.企业.AxdFirm;
import com.yuan.service.AxdAppUserService;
import com.yuan.service.企业.AxdFirmService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import com.yuan.pojo.*;
import org.springframework.web.multipart.MultipartFile;

/*
 *  @author 雨安
 *  类名： JsonController
 *  创建时间：2024/3/17
 */
@RestController
//@CrossOrigin //跨域处理
@Slf4j
public class JsonController {

    @Autowired
    private AxdAppUserService axdAppUserService;

    @Autowired
    private AxdFirmService axdFirmService;

    @Autowired
    private PasswordEncoder passwordEncoder;





    @Transactional //此注解只能对出现运行时异常的sql添加进行回滚
    @PostMapping(value ="/getfxptemployy",produces = "application/json; charset=UTF-8")
    public String xixi(@RequestBody String str,String doName) throws JsonProcessingException {
        Integer count = 0;
        try{
            //创建添加到数据的集合
            List<AxdAppUser> axdList = new LinkedList<>();
            //创建转换对象
            ObjectMapper toObj = new ObjectMapper();
            //得到转换对象
            EmployyDto jyb = toObj.readValue(str, EmployyDto.class);
            //得到数据
            List<Employy> data = jyb.getData();

            //循环打印
            for (Employy employy:data) {
                //判断遍历对象不为null
                if (employy!=null){
                    //创建添加对象
                    AxdAppUser axd = new AxdAppUser();
                    //判断
                    if (StringUtils.isNotBlank(employy.getSex())){
                        //判断是男还是女
                        if (employy.getSex().equals("男")){
                            axd.setSex("男");
                        }else{
                            axd.setSex("女");
                        }
                    }
                    //判断
                    if (StringUtils.isNotBlank(employy.getCoName())){
                        //创建条件构造器
                        AxdFirm firm = axdFirmService.
                                getOne(new QueryWrapper<AxdFirm>().eq("name", employy.getCoName()));
                        //判断企业不为null
                        if (firm!=null){
                            //设置企业id
                            axd.setFirmId(firm.getId());
                        }
                    }
                    //判断
                    if (StringUtils.isNotBlank(employy.getIdCard())){
                        //设置身份证id
                        axd.setIdNumber(employy.getIdCard());
                    }
                    //判断姓名是否为null
                    if (StringUtils.isNotBlank(employy.getDname())){
                        //设置昵称
                        axd.setNickName(employy.getDname());
                        //设置真实姓名
                        axd.setRealName(employy.getDname());
                    }
                    //设置默认值
                    axd.setUserType("FIRM");
                    axd.setStatus("0");
                    axd.setDeleteFlag("0");
                    axd.setCreateBy("admin");
                    axd.setCreateTime(new Date());
                    axd.setUpdateBy("admin");
                    axd.setUpdateTime(new Date());
                    axd.setAuditStatus(1);
                    axd.setAuditor("admin");
                    axd.setSubPlatform("1");
                    //创建密码字符串
                    String pwd = "";
                    //创建随机对象
                    Random random = new Random();
                    //遍历设置密码
                    for (int i = 0; i <= 5; i++) {
                        //遍历生成密码 1-10的数字
                        pwd+=random.nextInt(10);
                    }
                    //设置暂时可见密码
                    axd.setAdminPassword(pwd);
                    //设置密码
                    axd.setPassword(passwordEncoder.encode(pwd));
                    //判断
                    if (StringUtils.isNotBlank(employy.getPhone())){
                        //设置手机号
                        axd.setPhonenumber(employy.getPhone());
                        //此处手机号为账号
                        axd.setUserName(employy.getPhone());
                        //使用身份证进行数据查询
                        if (StringUtils.isNotBlank(employy.getIdCard())){
                            //查询
                            AxdAppUser one = axdAppUserService.getOne(new QueryWrapper<AxdAppUser>()
                                    .eq("id_number", employy.getIdCard()).last("limit 1"));
                            //判断非null
                            if (one != null){
                                System.out.println(one + "这是我的重复数据");
                            }else{
                                //累计添加数量
                                count +=1;
                                //将数据添加到集合中
                                axdList.add(axd);
                            }
                        }
                    }
                }
            }//foreach结束
            //执行添加sql
            axdAppUserService.saveBatch(axdList);
            //打印日志提示
            log.info("操作人：" + doName + "成功了" + "添加了数据条数：" + count);
            //返回响应结果
            return "成功";
        }catch (Exception e){
            //控制台打印异常
            e.printStackTrace();
            log.info("操作人：" + doName + "失败了");
            throw new RuntimeException("出现异常了");
        }
    }


    @PostMapping(value ="/getJybJson",produces = "application/json; charset=UTF-8")
    public String hehe(@RequestBody String str) throws JsonProcessingException {
         try{
             //遍历
             ObjectMapper toObj = new ObjectMapper();
             //得到转换对象
             JybDataDto jyb = toObj.readValue(str, JybDataDto.class);
             //得到数据
             List<JybData> rows = jyb.getRows();
             //循环打印
             rows.forEach(System.out::println);
             //返回消息提示
             return "成功";
         }catch (Exception e){
             throw new RuntimeException("出现异常");
         }
    }
}
